import 'package:get/get.dart';
import 'package:yysk_app/enum/app_enum.dart';
import 'package:yysk_app/model/pay_model/bank_card_list_model.dart';
import 'package:yysk_app/model/pay_model/bank_card_unbind_model.dart';
import 'package:yysk_app/net/api/pay_url.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/pages/nft_pages/pay_pages/add_bank_card_hf/add_bank_card_hf_page.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/manage_bank/add_bank_card_page.dart';
import 'package:yysk_app/utils/dialog_widget.dart';
import 'package:oktoast/oktoast.dart';

enum BankTabType { pay, withdraw }

class BankCardModel {
  final String bankName;
  final String cardType;
  final String cardNo;
  BankCardModel({
    required this.bankName,
    required this.cardType,
    required this.cardNo,
  });
}

class ManageBankController extends GetxController {
  var tabIndex = 0.obs;
  var payCards = RxList<BankCardListModel>();
  var withdrawCards = <dynamic>[{}, {}, {}].obs;
  var isWalletOpened = false.obs;
  var type = '1'.obs; //1个人，2企业,3个体工商户
  var channel = 'HUIFU'.obs; //支付类型 //HUIFU汇付
  void switchTab(int index) {
    tabIndex.value = index;
    getBankList();
  }

  @override
  void onInit() {
    super.onInit();
    type.value = Get.parameters['type'] ?? Get.arguments['type'];
    channel.value = Get.parameters['channel'] ?? Get.arguments['channel'];
    if (type.value != '1') {
      tabIndex.value = 1;
    }
    getBankList();
  }

  void checkWalletAndAddCard() {
    if (tabIndex.value == 0) {
      Get.toNamed(
        '${AddBankCardHfPage.path}?comeFrom=add',
        arguments: {'comeFrom': 'add'},
      );
    } else {
      if (type.value == '1') {
        Get.toNamed(
          '${AddBankCardPage.path}?comeFrom=add',
          arguments: {
            'comeFrom': 'add',
            'titleText':
                ((tabIndex.value == 1 && payCards.length == 1)
                    ? '修改结算卡'
                    : '添加结算卡'),
          },
        );
      } else if (type.value == '2') {
        Get.toNamed(
          '${AddBankCardPage.path}?comeFrom=add',
          arguments: {
            'comeFrom': 'add',
            'plat': 'ENTERPRISE',
            'titleText':
                ((tabIndex.value == 1 && payCards.length == 1)
                    ? '修改结算卡'
                    : '添加结算卡'),
          },
        );
      } else {
        Get.toNamed(
          '${AddBankCardPage.path}?comeFrom=add',
          arguments: {
            'comeFrom': 'add',
            'plat': 'INDIVIDUAL_BUSINESS',
            'titleText':
                ((tabIndex.value == 1 && payCards.length == 1)
                    ? '修改结算卡'
                    : '添加结算卡'),
          },
        );
      }
    }
  }

  Future jieban(String id) async {
    await http.netFetch<dynamic>(
      PayUrl.bankCardUnbind,
      method: Method.post,
      params: {'id': id},
      success: (res) {
        if (res.code == 200) {
          showToast('解绑成功');
          getBankList();
        }
      },
    );
  }

  void unbindCardTap(String id) {
    showConfirmDialog(
      context: Get.context!,
      message: '确定解除银行卡绑定吗？',
      actions: [
        ActionModel(label: '取消', onPressed: () {}),
        ActionModel(
          label: '确定',
          onPressed: () {
            jieban(id);
          },
        ),
      ],
    );
  }

  Future getBankList() async {
    await http.netFetch<List<BankCardListModel>>(
      PayUrl.bankCardList,
      method: Method.post,
      params: {
        'channel': channel.value,
        'plat':
            type.value == '1'
                ? 'USER'
                : (type.value == '2' ? 'ENTERPRISE' : 'INDIVIDUAL_BUSINESS'),
        'bindType': tabIndex.value == 0 ? 'PAY' : 'WITHDRAW',
      },
      success: (res) {
        payCards.value = res.data ?? [];
      },
    );
  }

  void openWallet() {
    isWalletOpened.value = true;
  }

  void addCard(BankCardModel card) {
    if (tabIndex.value == 0) {
      // payCards.add(card);
    } else {
      withdrawCards.add(card);
    }
  }
}
